package cn.com.blueInfo.bpm.common.entity;

import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.enums.BaseStatus;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 公共字段类
 */
@Data
@EqualsAndHashCode(callSuper = true)
public abstract class BaseEntity extends BaseExtend {

    public static final String ORDER_NUMBER = "orderNumber";
    public static final String TENANT_ID = "tenantId";
    public static final String APPLICATION_ID = "applicationId";
    public static final String STATUS = "status";
    public static final String DELETED = "deleted";
    public static final String DELETE_USER_ID = "deleteUserId";
    public static final String DELETE_TIME = "deleteTime";
    public static final String REMARK = "remark";
    public static final String CREATE_USER_ID = "createUserId";
    public static final String CREATE_TIME = "createTime";
    public static final String UPDATE_USER_ID = "updateUserId";
    public static final String UPDATE_TIME = "updateTime";

    /** ID */
    @TableId(type = IdType.INPUT)
    @Schema(description = "ID")
    private Long id;
    /** 排序码 */
    @TableField(fill = FieldFill.INSERT)
    @Schema(description = "排序码")
    private Integer orderNumber;
    /** 租户ID */
    @TableField(fill = FieldFill.INSERT)
    @Schema(description = "租户ID")
    private Long tenantId;
    /** 租户系统ID */
    @TableField(fill = FieldFill.INSERT)
    @Schema(description = "租户系统ID")
    private Long applicationId;
    /** 数据状态：1-已启用，2-未启用，3-待定 */
    @TableField(fill = FieldFill.INSERT)
    @Schema(description = "数据状态：1-已启用，2-未启用，3-待定")
    private BaseStatus status;
    /** 删除状态：1-已删除，0-未删除 */
    @TableField(fill = FieldFill.INSERT)
    @Schema(description = "删除状态：1-已删除，0-未删除")
    private BaseDeleted deleted;
    /** 删除人ID */
    @TableField(fill = FieldFill.UPDATE)
    @Schema(description = "删除人ID")
    private Long deleteUserId;
    /** 删除时间 */
    @TableField(fill = FieldFill.UPDATE)
    @Schema(description = "删除时间")
    private LocalDateTime deleteTime;
    /** 更新说明 */
    @Schema(description = "更新说明")
    private String remark;
    /** 创建人ID */
    @TableField(fill = FieldFill.INSERT)
    @Schema(description = "创建人ID")
    private Long createUserId;
    /** 创建时间 */
    @TableField(fill = FieldFill.INSERT)
    @Schema(description = "创建时间")
    private LocalDateTime createTime;
    /** 更新人ID */
    @TableField(fill = FieldFill.UPDATE)
    @Schema(description = "更新人ID")
    private Long updateUserId;
    /** 更新时间 */
    @TableField(fill = FieldFill.UPDATE)
    @Schema(description = "更新时间")
    private LocalDateTime updateTime;

    public static <T extends BaseEntity> Map<String, SFunction<T, ?>> getCommonColumn(
            SFunction<T, Integer> orderNumberFunc,
            SFunction<T, Long> tenantIdFunc,
            SFunction<T, Long> applicationIdFunc,
            SFunction<T, BaseStatus> statusFunc,
            SFunction<T, BaseDeleted> deletedFunc,
            SFunction<T, Long> deleteUserIdFunc,
            SFunction<T, LocalDateTime> deleteTimeFunc,
            SFunction<T, String> remarkFunc,
            SFunction<T, Long> createUserIdFunc,
            SFunction<T, LocalDateTime> createTimeFunc,
            SFunction<T, Long> updateUserIdFunc,
            SFunction<T, LocalDateTime> updateTimeFunc,
            SFunction<T, String> extend1Func,
            SFunction<T, String> extend2Func,
            SFunction<T, JSONObject> extend3Func) {
        Map<String, SFunction<T, ?>> columnMap = new HashMap<>();
        columnMap.put(BaseEntity.ORDER_NUMBER, orderNumberFunc);
        columnMap.put(BaseEntity.TENANT_ID, tenantIdFunc);
        columnMap.put(BaseEntity.APPLICATION_ID, applicationIdFunc);
        columnMap.put(BaseEntity.STATUS, statusFunc);
        columnMap.put(BaseEntity.DELETED, deletedFunc);
        columnMap.put(BaseEntity.DELETE_USER_ID, deleteUserIdFunc);
        columnMap.put(BaseEntity.DELETE_TIME, deleteTimeFunc);
        columnMap.put(BaseEntity.REMARK, remarkFunc);
        columnMap.put(BaseEntity.CREATE_USER_ID, createUserIdFunc);
        columnMap.put(BaseEntity.CREATE_TIME, createTimeFunc);
        columnMap.put(BaseEntity.UPDATE_USER_ID, updateUserIdFunc);
        columnMap.put(BaseEntity.UPDATE_TIME, updateTimeFunc);
        columnMap.put(BaseExtend.EXTEND_1, extend1Func);
        columnMap.put(BaseExtend.EXTEND_2, extend2Func);
        columnMap.put(BaseExtend.EXTEND_3, extend3Func);
        return columnMap;
    }

}
